package com.coffeeloversupportlibrary.coffeelover.domain.article.cotegory;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.contentshow.Details;
import com.coffeeloversupportlibrary.coffeelover.domain.article.entity.Article;
import com.coffeeloversupportlibrary.databinding.CategoryViewItemBinding;

import java.util.List;

/**
 * @author puyv
 * @createTime 2021/11/12 8:43
 * @projectName CoffeeLoverSupportLibrary
 * @className CategoryListAdapter.java
 * @description TODO  文章分类模块下 一个分页子页面所持有 RecyclerView的数据适配器
 *
 */
public class CategoryListAdapter extends RecyclerView.Adapter {
    private List<Article> lists;
    private CategoryViewItemBinding categoryViewItemBinding;


    public CategoryListAdapter(){}
    public CategoryListAdapter(List<Article> list){
        this.lists=list;
    }

    //为加载更多做准备
    public List<Article> getLists() {
        return lists;
    }

    public void setLists(List<Article> lists) {
        this.lists = lists;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_view_item,parent,false);
        categoryViewItemBinding= DataBindingUtil.bind(view);
        return new CategoryArticleViewHolder(categoryViewItemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Article article=lists.get(position);

        Glide.with(holder.itemView.getContext())
                .load(article.getArticleBanner())
                .into(((CategoryArticleViewHolder)holder).categoryViewItemBinding
                .shareItemImage);

        ((CategoryArticleViewHolder)holder)
                .categoryViewItemBinding
                .shareItemContent
                .setText(article.getArticleTitle());


        ((CategoryArticleViewHolder)holder)
                .categoryViewItemBinding
                .shareItemImage
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(holder.itemView.getContext(), Details.class);
                        intent.putExtra("articleId",article.getArticleId());
                        holder.itemView.getContext()
                                .startActivity(intent);
                    }
                });


    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    static class CategoryArticleViewHolder extends RecyclerView.ViewHolder{
        CategoryViewItemBinding categoryViewItemBinding;
        public CategoryArticleViewHolder(@NonNull CategoryViewItemBinding categoryViewItemBinding) {
            super(categoryViewItemBinding.getRoot());
            this.categoryViewItemBinding=categoryViewItemBinding;
        }
    }
}
